﻿using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;



namespace helicopterFight
{
    public class Minimap
    {
        private Texture2D bkg, dotGreen, dotRed, dotTomb;
        private Game1 game;
        private List<GameObject> gameObjects;
        private RenderTarget2D renderTarget;

        int worldWidth, worldHeight;
        float scale;
        int width, height;
        int tx;
        int slicesX, slicesY;

        public Minimap(Game1 game, List<GameObject> gameObjects)
        {
            this.game = game;
            this.gameObjects = gameObjects;
            bkg = game.Content.Load<Texture2D>("indicators/mapbkg");
            dotGreen = game.Content.Load<Texture2D>("indicators/dotgreen");
            dotRed = game.Content.Load<Texture2D>("indicators/dotred");
            dotTomb = game.Content.Load<Texture2D>("indicators/rip");

            // FIXME TODO: Fajnie by bylo nie miec tego tu wbitego na stale.
            worldWidth = 7500;
            worldHeight = 7500;

            // Szerokosc mapy, ustalana arbitralnie
            width = 100;

            // Skala mapy, mnozymy wartosci w swiecie przez to i mamy pkt na mapie
            scale = (float)width / (float)worldWidth; 

            // Wysokosc, ustawiana proporcjonalnie do szerokosci
            height = (int)(scale * (float)worldHeight);

            // Lewy-gorny rog minimapy.
            tx = game.graphics.PreferredBackBufferWidth - width - 20;

            slicesX = width / bkg.Width + (width % bkg.Width > 0 ? 1 : 0);
            slicesY = height / bkg.Height + (height % bkg.Height > 0 ? 1 : 0);

            renderTarget = new RenderTarget2D(game.GraphicsDevice,
                width,
                height,
                1,
                game.GraphicsDevice.PresentationParameters.BackBufferFormat);
        }

        public Texture2D draw(SpriteBatch sb)
        {


            game.GraphicsDevice.SetRenderTarget(0, renderTarget);
            game.GraphicsDevice.Clear(Color.White);



            sb.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);

            for (int i = 0; i < slicesX; i++)
            {
                for (int j = 0; j < slicesY; j++)
                {
                    sb.Draw(bkg, new Rectangle(/*tx +*/ i * bkg.Width, /*ty +*/ j * bkg.Height, bkg.Width, bkg.Height), Color.White);
                }
            }

            //SpriteBatch sprite = new SpriteBatch(game.GraphicsDevice);
            //sprite.Begin(SpriteBlendMode.None, SpriteSortMode.Texture, SaveStateMode.SaveState);
            //Texture2D tex2d = new Texture2D(game.GraphicsDevice, width, height);


            foreach (GameObject ob in gameObjects)
            {
                if (!(ob is HelicopterObject))
                    continue;

                HelicopterObject heli = ob as HelicopterObject;

                //if (!heli.IsAlive)
                //    continue;

                Texture2D dot;

                if (!heli.IsAlive)
                {
                    dot = dotTomb;
                }
                else
                    if (heli == game.testState.helikopter1)
                    {
                        dot = dotGreen;
                    }
                    else
                    {
                        dot = dotRed;
                    }

                sb.Draw(dot, new Rectangle(
                    /*tx +*/ (width - (int)(scale * (float)heli.position.X)),
                    /*ty +*/ (int)(-scale * (float)heli.position.Z),
                    dot.Width, dot.Height),
                    Color.White);


            }
            sb.End();
            
            game.GraphicsDevice.SetRenderTarget(0, null);


            return renderTarget.GetTexture();
            //sprite.End();
            
            
        }
    }
}
